﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ComicViewer.IManhua;
using System.Net;
using System.IO;
using System.Windows.Threading;
using System.Threading;
using System.ComponentModel;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Animation;

namespace ComicViewer
{
    /// <summary>
    /// Interaction logic for PageFunction3.xaml
    /// </summary>
    public partial class ViewPage2 : PageFunction<String>
    {
        ComicTable m_table;
        private int _index = 0;
        public ViewPage2()
        {
            InitializeComponent();

            _panelPreview.Visibility = System.Windows.Visibility.Collapsed;

            m_table = Application.Current.Properties["Table"] as ComicTable;
            this.WindowTitle = m_table.Name;
            this.Loaded += new RoutedEventHandler(ViewPage_Loaded);
        }


        void ViewPage_Loaded(object sender, RoutedEventArgs e)
        {
            _list.Children.Clear();

            _btnCaption.Content = m_table.Name;
            _index = 0;
            if (m_table.Pages.Count == 0)
            {
               new IManhuaProxy().GetPages(m_table);
            }

            foreach (ComicPage page in m_table.Pages)
            {
                Border border = new Border();
                border.Margin = new Thickness(5, 5, 0, 0);
                border.BorderBrush = new SolidColorBrush(Colors.Yellow);
                border.BorderThickness = new Thickness(3);
                Image img = new Image();
                img.Width = 80;
                img.Height = 100;
                img.Tag = page;
                img.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(img_PreviewMouseLeftButtonUp);

                border.Child = img;
                _list.Children.Add(border);
                WebClient client = new WebClient();
                client.Headers.Add("Referer", page.ReferUrl);
                client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
                client.DownloadDataAsync(page.PicUri, img);
            }
        }

        void client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                ComicPage page = (e.UserState as Image).Tag as ComicPage;
                
                Stream stream = new MemoryStream(e.Result);
                BitmapImage source = new BitmapImage();
                source.BeginInit();
                source.StreamSource = stream;
                source.EndInit();
                page.Source = source;
                (e.UserState as Image).Source = page.Source;
            }
            else
            {
                ComicPage page = (e.UserState as Image).Tag as ComicPage;
                
                WebClient client = new WebClient();
                client.Headers.Add("Referer", page.ReferUrl);
                client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted2);
                client.DownloadDataAsync(page.PicUriPng, e.UserState);
            }
        }

        void client_DownloadDataCompleted2(object sender, DownloadDataCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                ComicPage page = (e.UserState as Image).Tag as ComicPage;

                Stream stream = new MemoryStream(e.Result);
                BitmapImage source = new BitmapImage();
                source.BeginInit();
                source.StreamSource = stream;
                source.EndInit();
                page.Source = source;
                (e.UserState as Image).Source = page.Source;
            }
            else
            {
            }
        }

        void img_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Image img = sender as Image;
            ComicPage page = img.Tag as ComicPage;

            _index = page.Index;
            _panelPreview.Visibility = System.Windows.Visibility.Visible;
            _panelPreview.DataContext = page;
        }

        private void _panelPreview_PreviewMouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            _panelPreview.Visibility = System.Windows.Visibility.Collapsed;
            _panelPreview.DataContext = null;
        }

        private void GoPrev()
        {
            if (_index > 0)
            {
                _index--;
            }
            _panelPreview.DataContext = m_table.Pages[_index];
            _ctlScroll.ScrollToTop();
        }

        private void GoNext()
        {
            if (_index < m_table.Pages.Count-1)
            {
                _index++;
            }
            _panelPreview.DataContext = m_table.Pages[_index];
            _ctlScroll.ScrollToTop();
        }

        protected override void OnPreviewKeyUp(KeyEventArgs e)
        {
            base.OnPreviewKeyUp(e);
            if (e.Key == Key.Left)
            {
                GoPrev();
            }
            else if (e.Key == Key.Right)
            {
                GoNext();
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            GoPrev();

        }
        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            GoNext();

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            m_table = Application.Current.Properties["Table"] as ComicTable;

            for (int index = 0; index < m_table.Book.Tables.Count; index++)
            {
                if (m_table.Book.Tables[index] == m_table && index >0)
                {
                    m_table = m_table.Book.Tables[index - 1];
                    Application.Current.Properties["Table"] = m_table;
                    this.WindowTitle = m_table.Name;
                    ViewPage_Loaded(null, null);
                    break;
                }
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new BookDetailPage());
        }
    }
}
